package spark_graphx.spark_graphx

import java.io.PrintWriter
import scala.io.Source
import java.io.File

object PreDealData {

  def main(args: Array[String]): Unit = {
    var file = Source.fromFile("D:/文档/graphx/data/user-imei-tmp.txt")
    var writer = new PrintWriter(new File("D:/文档/graphx/data/user-imei.txt"))
    var nr = 0;
    var imei_map: Map[String, Int] = Map()
    for (line <- file.getLines) {
      if (line != "") {
        val fields = line.split("\t", -1)
        val f1 = fields(0)
        val f2 = if (fields.length > 1) fields(1) else ""
        var imei_id = 0
        if (!imei_map.contains(f2)) {
          nr += 1
          imei_map += (f2 -> nr)
          imei_id = nr;
        }else{
          imei_id = imei_map(f2)
        }
        writer.println(s"${f1}\t${imei_id}\t$f2")
      }
    }
    writer.close()
    file.close
    
    file = Source.fromFile("D:/文档/graphx/data/driver-imei-tmp.txt")
    writer = new PrintWriter(new File("D:/文档/graphx/data/driver-imei.txt"))
  
    for (line <- file.getLines) {
      if (line != "") {
        val fields = line.split("\t", -1)
        val f1 = fields(0)
        val f2 = if (fields.length > 1) fields(1) else ""
        var imei_id = 0
        if (!imei_map.contains(f2)) {
          nr += 1
          imei_map += (f2 -> nr)
          imei_id = nr;
        }else{
          imei_id = imei_map(f2)
        }
        writer.println(s"${f1}\t${imei_id}\t$f2")
      }
    }
    writer.close()
    file.close
    dealPayID()
  }
  
  def dealPayID(){
        var file = Source.fromFile("D:/文档/graphx/data/user-pay-id-tmp.txt")
    var writer = new PrintWriter(new File("D:/文档/graphx/data/user-pay-id.txt"))
    var nr = 0;
    var imei_map: Map[String, Int] = Map()
    for (line <- file.getLines) {
      if (line != "") {
        val fields = line.split("\t", -1)
        val f1 = fields(0)
        val f2 = if (fields.length > 1) fields(1) else ""
        var imei_id = 0
        if (!imei_map.contains(f2)) {
          nr += 1
          imei_map += (f2 -> nr)
          imei_id = nr;
        }else{
          imei_id = imei_map(f2)
        }
        writer.println(s"${f1}\t${imei_id}\t$f2")
      }
    }
    writer.close()
    file.close
  }
}